//
//  BNRDetailViewController.m
//  Quiz
//
//  Created by YaoJacob on 15/11/4.
//  Copyright © 2015年 Roger. All rights reserved.
//

#import "BNRDetailViewController.h"
#import "BNRItem.h"
#import "BNRImageStore.h"

@interface BNRDetailViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *serialNumberField;
@property (weak, nonatomic) IBOutlet UITextField *valueField;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
//@property (weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;
@property (strong, nonatomic) UIPopoverController* imagePickerPopover;

@end

@implementation BNRDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    UIInterfaceOrientation io = [[UIApplication sharedApplication]statusBarOrientation];
    [self prepareViewsForOrientation:io];
    
    BNRItem *item = self.item;
    
    self.nameField.text = item.itemName;
    self.serialNumberField.text = item.serialNumber;
    self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];
    
    static NSDateFormatter *dateFormater = nil;
    if (!dateFormater) {
        dateFormater =[[NSDateFormatter alloc]init];
        dateFormater.dateStyle = NSDateFormatterMediumStyle;
        dateFormater.timeStyle = kCFDateFormatterNoStyle;
    }
    
    self.dateLabel.text = [dateFormater stringFromDate:item.dateCreated];
    
    self.imageView.image = [[BNRImageStore sharedStore] imageForKey:item.itemKey];
    
}
- (IBAction)deleImage:(id)sender {
    [[BNRImageStore sharedStore] deleteImageForKey:self.item.itemKey];
    self.imageView.image = nil;
}
- (IBAction)backgroundTapped:(id)sender {
    [[self view] endEditing:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.view endEditing:YES];
    
    BNRItem* item = self.item;
    item.itemName =self.nameField.text;
    item.serialNumber = self.serialNumberField.text;
    item.valueInDollars = [self.valueField.text intValue];
}

#pragma Seting. -
-(void)setItem:(BNRItem *)item
{
    _item = item;
    
    self.navigationItem.title = item.itemName;
}

-(IBAction)closeKeyBoards:(id)sender
{
    self.editing = FALSE;
}

#pragma 拍照
//
- (IBAction)takePicture:(id)sender {
    
    if ([self.imagePickerPopover isPopoverVisible]) {
        [self.imagePickerPopover dismissPopoverAnimated:YES];
        self.imagePickerPopover = nil;
        return;
    }
    
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    
    //    vailableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceFront
    //    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
    }
    else
    {
        imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    }
    

    //Front or back camera.
    imagePicker.delegate = self;
    //    imagePicker.cameraOverlayView
    
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        self.imagePickerPopover = [[UIPopoverController alloc]initWithContentViewController:imagePicker];
        self.imagePickerPopover.delegate = self;
        
//        self.imagePickerPopover.popoverBackgroundViewClass = 
        
        [self.imagePickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    else
    {
        [self presentViewController:imagePicker animated:YES completion:nil]; //ShowDialog
    }
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    self.imageView.image = image;
    [[BNRImageStore sharedStore] setImage:image forKey:self.item.itemKey];
    
    if (self.imagePickerPopover) {
        [self.imagePickerPopover dismissPopoverAnimated:YES];
        self.imagePickerPopover = nil;
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];//closeDialog
    }
    
}

//点击空白处，即可关闭键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma 自动旋转
-(void)prepareViewsForOrientation:(UIInterfaceOrientation)orientation
{
    //IPAD
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        return;
    }
    
//    if (UIInterfaceOrientationIsLandscape(orientation)) {
//        self.imageView.hidden = YES;
//        self.cameraButton.enabled = NO;
//    }
//    else
//    {
//        self.imageView.hidden = NO;
//        self.cameraButton.enabled = YES;
//    }
}

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    NSLog(@"popover ControllerDidDismissPopover");
    self.imagePickerPopover = nil;
}
@end
